//
//  SEGHotLineManagerCollectionViewTableViewCell.m
//  Commom
//
//  Created by 梁学彰 on 2020/3/16.
//

#import "SEGHotLineManagerCollectionViewTableViewCell.h"
#import "CommomHeader.h"
#import "SEGPlatformHeader.h"


#define kPageMaxWidth 60

@interface SEGHotLineManagerCollectionViewTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,SEGHotLineDelegate>
@property (nonatomic, strong) UIView *contentWhiteView;
@property (nonatomic, strong) UICollectionView *collect;
@property (nonatomic, strong) UICollectionView *pageCollect;

@property (nonatomic, assign) NSUInteger currentPage;
@end

@implementation SEGHotLineManagerCollectionViewTableViewCell
#pragma mark - life cycle
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubViews];
    }
    return self;
}


#pragma mark - init
- (void)initSubViews {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentWhiteView = [UIView new];
    self.contentWhiteView.backgroundColor = kColor_W;
    [self.contentView addSubview:self.contentWhiteView];
    
    [self.contentWhiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(5);
        make.bottom.equalTo(self.contentView).offset(-5);
        make.left.equalTo(self.contentView).offset(20);
        make.right.equalTo(self.contentView).offset(-20);
    }];
    
    self.contentWhiteView.layer.shadowColor = kColor_L2.CGColor;
    self.contentWhiteView.layer.shadowOpacity = 0.36f;
    self.contentWhiteView.layer.cornerRadius = 5;
    self.contentWhiteView.layer.shadowOffset = CGSizeMake(0,0);
    
    [self initMainCollect];
    [self initPageCollect];
    
}

- (void)initMainCollect {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    self.collect = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    self.collect.dataSource = self;
    self.collect.delegate = self;
    self.collect.bounces = NO;
    self.collect.pagingEnabled = YES;
    self.collect.showsHorizontalScrollIndicator = NO;
    self.collect.showsVerticalScrollIndicator = NO;
    self.collect.layer.cornerRadius = 5;
    self.collect.layer.masksToBounds = YES;
    self.collect.backgroundColor = kColor_W;
    [self.contentWhiteView addSubview:self.collect];
    
    [self.collect registerClass:[SEGHotLineManagerCollectionViewCell class] forCellWithReuseIdentifier:@"SEGHotLineManagerCollectionViewCell"];
    
    [self.collect mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentWhiteView);
    }];
}

- (void)initPageCollect {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.pageCollect = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    self.pageCollect.dataSource = self;
    self.pageCollect.delegate = self;
    self.pageCollect.bounces = NO;
    self.pageCollect.userInteractionEnabled = NO;
    self.pageCollect.showsHorizontalScrollIndicator = NO;
    self.pageCollect.showsVerticalScrollIndicator = NO;
    self.pageCollect.backgroundColor = [UIColor clearColor];
    [self.contentWhiteView addSubview:self.pageCollect];
    
    [self.pageCollect registerClass:[SEGHotLineManagerPageCollectionViewCell class] forCellWithReuseIdentifier:@"SEGHotLineManagerPageCollectionViewCell"];
    
    [self.pageCollect mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentWhiteView).offset(-10);
        make.height.mas_equalTo(3);
        make.width.mas_equalTo(6);
        make.centerX.equalTo(self.contentWhiteView);
    }];
}

#pragma mark - send request

#pragma mark - custom delegate

#pragma mark - UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.collect) {
        SEGHotLineManagerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SEGHotLineManagerCollectionViewCell" forIndexPath:indexPath];
        cell.model = self.dataArray[indexPath.item];
        cell.delegate = self;
        return cell;
    }
    SEGHotLineManagerPageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SEGHotLineManagerPageCollectionViewCell" forIndexPath:indexPath];
    cell.isSelect = (self.currentPage == indexPath.item);
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.collect) {
        return CGSizeMake(LYScreenWidth() - 40, LYScreenWidth() - 80 + 104 + 30);
    }
    if (indexPath.item == self.currentPage) {
        return CGSizeMake(10, 3);
    }
    return CGSizeMake(5, 3);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if (collectionView == self.collect) {
        return 0;
    }
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if (collectionView == self.collect) {
        return 0;
    }
    return 5;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"scrollViewDidEndDecelerating");
    self.currentPage = self.collect.contentOffset.x/self.collect.width;
    [self.pageCollect performBatchUpdates:^{
        [self.pageCollect reloadData];
        [self.pageCollect scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.currentPage inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    } completion:^(BOOL finished) {
        [self.pageCollect reloadData];
    }];
    
}



#pragma mark - event response

#pragma mark - SEGHotLineDelegate
- (void)callActionWithView:(UIView *)view tel:(NSString *)tel {
    if (tel.length <= 0) {
        //只有管家会有这种情况
        [SEGAlertView showTmepMessageWithMessage:@"暂未更新管家联系方式"];
        return;
    }
    SEGNewAlertView *alertView = [[SEGNewAlertView alloc] init];
    [alertView showWithAlertTitle:nil text:[NSString stringWithFormat:@"%@",tel] buttonTitlesArray:@[@"取消",@"呼叫"] actionBlock:^(NSInteger btnIndex) {
        if (btnIndex == 1) {
            if (@available(iOS 8.0, *)) {
                    NSString *str = [NSString stringWithFormat:@"telprompt://%@",tel];
                if (@available(iOS 10.0, *)) {
                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];
                    } else {
                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
                }
            }
        }
    }];
}


#pragma mark - setter and getter
- (void)setDataArray:(NSMutableArray<SEGServiceHotlineModel *> *)dataArray {
    _dataArray = dataArray;
    [self.collect reloadData];
    self.pageCollect.hidden = NO;
    [self.pageCollect reloadData];
    if (_dataArray.count > 1) {
        CGFloat width = 10*dataArray.count;
        if (10*dataArray.count > kPageMaxWidth) {
            width = kPageMaxWidth;
        }
        [self.pageCollect mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(width);
        }];
    }else {
        self.pageCollect.hidden = YES;
    }
}

@end


@interface SEGHotLineManagerCollectionViewCell ()
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *telLabel;
@property (nonatomic, strong) UIButton *callBtn;
@end

@implementation SEGHotLineManagerCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    self.iconImageView = [UIImageView new];
    self.iconImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:self.iconImageView];
    self.iconImageView.layer.masksToBounds = YES;
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(30);
        make.centerX.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(20);
        make.width.height.mas_equalTo(LYScreenWidth() - 80);
    }];
    
    
    self.titleLabel = [UILabel new];
    self.titleLabel.text = @"#333333";
    self.titleLabel.font = [kFont_18 fontWithBold];
    self.titleLabel.textColor = kColor_1;
    [self.contentView addSubview:self.titleLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImageView.mas_bottom).offset(29);
        make.height.mas_equalTo(20);
        make.left.equalTo(self.iconImageView);
        make.width.mas_lessThanOrEqualTo(LYScreenWidth() - 80 - 40 - 60);
    }];
    
    UILabel *label = [UILabel new];
    label.text = @"管家";
    label.backgroundColor = kColor_T;
    label.font = kFont_12;
    label.layer.masksToBounds = YES;
    label.layer.cornerRadius = 8;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLabel);
        make.left.equalTo(self.titleLabel.mas_right).offset(5);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(16);
    }];
    
    self.telLabel = [UILabel new];
    self.telLabel.font = kFont_15 ;
    self.telLabel.text = @"#333333";
    self.telLabel.textColor = kColor_3;
    [self.contentView addSubview:self.telLabel];
    
    [self.telLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(9);
        make.height.mas_equalTo(17);
        make.left.equalTo(self.iconImageView);
        make.width.mas_lessThanOrEqualTo(LYScreenWidth() - 80 - 60);
    }];
    
    self.callBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.callBtn setImage:[UIImage imageNamed:SEGPicName(@"icon_btn_hotline_call")] forState:UIControlStateNormal];
    [self.contentView addSubview:self.callBtn];
    
    [self.callBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImageView.mas_bottom).offset(32.5);
        make.right.equalTo(self.contentView).offset(-22);
        make.height.width.mas_equalTo(36);
    }];
    
    [self.callBtn addTarget:self action:@selector(callAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)callAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(callActionWithView:tel:)]) {
        [self.delegate callActionWithView:self tel:self.telLabel.text];
    }
}


- (void)setModel:(SEGServiceHotlineModel *)model {
    _model = model;
    self.titleLabel.text = model.userName;
    NSURL *iconUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL, model.icoPath]];
    [self.iconImageView setImageWithURL:iconUrl
                       placeholderImage:[UIImage imageNamed:SEGPicName(@"pic_guanjia_head")]];
    self.telLabel.text = model.tel;
}

@end

@implementation SEGHotLineManagerPageCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    self.contentBackView = [UIView new];
    self.contentBackView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.contentBackView];
    self.contentBackView.layer.masksToBounds = YES;
    self.contentBackView.layer.cornerRadius = 1.5;
    self.contentBackView.backgroundColor = [kColor_C colorWithAlphaComponent:0.7];
    
    [self.contentBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
}

- (void)setIsSelect:(BOOL)isSelect {
    if (isSelect) {
        self.contentBackView.backgroundColor = kColor_3;
    }else {
        self.contentBackView.backgroundColor = [kColor_C colorWithAlphaComponent:0.3];
    }
}

@end
